package edu.bu.cs673.superkids123.children.view.lessons;

import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JPanel;

import edu.bu.cs673.superkids123.AbstractFrame;
import edu.bu.cs673.superkids123.AbstractNavigationView;
import edu.bu.cs673.superkids123.children.controller.lessons.AddingLessonController;

/**
 * Adding CountingLessonView
 * 
 * Supports the view part of the MVC pattern for a simple addition lesson for a
 * young child. Audio and visual cues provide an enhanced learning experience.
 * 
 * @author Ted Atkinson
 */

public class AddingLessonView extends AbstractNavigationView implements
	ActionListener {
	
	// Display two numbers to add and their result (e.g. 1 + 1 = 2)
	public static final int ITEMS_IN_ADDING_PANEL = 5;
	private boolean readyToPaint = false;

	private JPanel addingPanel;
	private JPanel incrementPanel;
	private JPanel numberPanel;
	private JPanel decrementPanel;
	private JPanel ctrlButtonActionPanel;
	private JButton[] numberButtons = new JButton[ITEMS_IN_ADDING_PANEL];
	private JButton playButton;
	private JButton stopButton;
	private JButton lessButtonLeft;
	private JButton moreButtonLeft;
	private JButton lessButtonRight;
	private JButton moreButtonRight;
	//private JPanel graphicsPanel;
	private boolean isBusy = false;
	
	public AddingLessonView(AbstractFrame mainFrame) {
		super(mainFrame);
	}
	
	@Override
	protected JPanel layout() {
		this.setAvatarVisible(true);
		this.setParentsButtonVisible(true);
		this.setTitleBorderVisible(false);
		
		addingPanel = new JPanel() {
			private static final long serialVersionUID = 2L;
			
			public void paint(Graphics g) {
				super.paint(g);
				if (readyToPaint) {
					int positionsX[] = new int[5];
					for (int i=0; i < positionsX.length; i++) {
						positionsX[i] = numberButtons[i].getLocation().x + 25;
					}

					int positionsY[] = new int[4];
					positionsY[0] = addingPanel.getLocation().y;
					positionsY[1] = positionsY[0] + 30;
					positionsY[2] = positionsY[1] + 30;
					positionsY[3] = positionsY[2] + 30;
				}
			}
		};
		
		addingPanel.setLayout(new GridBagLayout());
		getMainFrame().getController(AddingLessonController.class).startLesson();
		
		GridBagConstraints c = new GridBagConstraints();
		c.gridx = 0;
		c.gridy = 0;
		c.gridwidth = 1;
		c.gridheight = 1;
		c.weightx = 1;
		c.weighty = 0;
		c.insets = new Insets(5, 5, 5, 15);
		c.fill = GridBagConstraints.HORIZONTAL;
		c.anchor = GridBagConstraints.NORTHWEST;
		
		// Buttons for incrementing each of the addends on the screen
		GridBagConstraints c2 = new GridBagConstraints();
		c2.gridx = 0;
		c2.gridy = 0;
		c2.gridwidth = 1;
		c2.gridheight = 1;
		c2.weightx = 1;
		c2.weighty = 0;
		c2.insets = new Insets(5, 70, 5, 70);
		c2.fill = GridBagConstraints.NONE;
		c2.anchor = GridBagConstraints.WEST;
		
		incrementPanel = new JPanel();
		incrementPanel.setLayout(new GridBagLayout());
		
		moreButtonLeft = new JButton("+");
		moreButtonRight = new JButton("+");
		
		incrementPanel.add(moreButtonLeft, c2);
		c2.anchor = GridBagConstraints.CENTER;
		incrementPanel.add(moreButtonRight,c2);
		c2.anchor = GridBagConstraints.WEST;
		
		moreButtonLeft.addActionListener(this);
		moreButtonRight.addActionListener(this);
		
		addingPanel.add(incrementPanel, c);
		c.gridy++;
		
		// show the number images as buttons in a child numberPanel using
		// FlowLayout
		numberPanel = new JPanel();
		numberPanel.setLayout(new FlowLayout());

		initializeAddingButtons();
		showAddingButtons();
		
		addingPanel.add(numberPanel, c);
		c.gridy++;

		// Buttons for decrementing each of the addends on the screen
		decrementPanel = new JPanel();
		decrementPanel.setLayout(new GridBagLayout());
		
		lessButtonLeft = new JButton("-");
		lessButtonRight = new JButton("-");

		decrementPanel.add(lessButtonLeft, c2);
		c2.anchor = GridBagConstraints.CENTER;
		decrementPanel.add(lessButtonRight, c2);
		c2.anchor = GridBagConstraints.WEST;
		
		lessButtonLeft.addActionListener(this);
		lessButtonRight.addActionListener(this);

		addingPanel.add(decrementPanel, c);
		c.gridy++;
		
		// Play and stop buttons for the activity
		ctrlButtonActionPanel = new JPanel();
		ctrlButtonActionPanel.setLayout(new FlowLayout());
		
		playButton = new JButton("Add!");
		stopButton = new JButton("Stop Playing");
		
		ctrlButtonActionPanel.add(playButton);
		ctrlButtonActionPanel.add(stopButton);
		
		playButton.addActionListener(this);
		stopButton.addActionListener(this);

		c.insets = new Insets(5, 5, 15, 15);
		c.fill = GridBagConstraints.NONE;
		c.anchor = GridBagConstraints.EAST;
		c.weighty = 0;
		addingPanel.add(ctrlButtonActionPanel, c);		
			
		readyToPaint = true;
		return addingPanel;
	}
	
	// Get the number buttons ready with their current images
	public void initializeAddingButtons() {
		readyToPaint = false;
		int leftNumber = getMainFrame().getController(
				AddingLessonController.class).getLeftNumber();
		int rightNumber = getMainFrame().getController(
				AddingLessonController.class).getRightNumber();
		int sum = getMainFrame().getController(
				AddingLessonController.class).getSum();

		for (int i = 0; i < ITEMS_IN_ADDING_PANEL; i++) {
			numberButtons[i] = new JButton();
			int addNum = -1;
			String addSymbol = " ";
			ImageIcon addingImageIcon;
			
			switch (i) {
            case 0:
            	addNum = leftNumber;
            	break;
            case 1:
            	addSymbol = "plus";
            	break;
            case 2:
            	addNum = rightNumber;
            	break;
            case 3:
            	addSymbol = "equals";
            	break;
            case 4:
				addNum = sum;
				break;
			}
	
			if (addNum >= 0) {
				addingImageIcon = getMainFrame().getController(
					AddingLessonController.class).getImageIcon(addNum);
			}
			else
			{
				addingImageIcon = getMainFrame().getController(
						AddingLessonController.class).getImageIcon(addSymbol);
			}
			int width = addingImageIcon.getIconWidth();
			int height = addingImageIcon.getIconHeight();
			numberButtons[i].setBounds(new Rectangle(width, height));
			numberButtons[i].setIcon(addingImageIcon);
			numberButtons[i].addActionListener(this);
		}
	}
		
	public void showAddingButtons() {
		numberPanel.removeAll();
		for (int i = 0; i < ITEMS_IN_ADDING_PANEL; i++) {
			numberPanel.add(numberButtons[i]);
		}
		readyToPaint = true;
		addingPanel.revalidate();
		addingPanel.repaint();
	}

	public void repaintAddingPanel() {
		readyToPaint = true;
		addingPanel.revalidate();
		addingPanel.repaint();
	}

	public void setBusy(boolean busyState) {
		isBusy = busyState;
	}
	
	/**
	 * actionPerformed Delegate the responsibility of the action to the
	 * controller
	 */
	public void actionPerformed(ActionEvent e) {
		if (e.getSource() instanceof JButton) {
			
			if (e.getSource() == stopButton) {
				getMainFrame().getController(AddingLessonController.class)
						.setStopPlay(true);
			}

			// If the user is currently playing, then return
			if (isBusy) {
				return;
			}

			// manage control buttons			
			else if (e.getSource() == moreButtonLeft) {
				getMainFrame().getController(AddingLessonController.class)
						.moreButtonLeftPressed(this);
			} else if (e.getSource() == lessButtonLeft) {
				getMainFrame().getController(AddingLessonController.class)
						.lessButtonLeftPressed(this);
			} else if (e.getSource() == moreButtonRight) {
				getMainFrame().getController(AddingLessonController.class)
						.moreButtonRightPressed(this);
			} else if (e.getSource() == lessButtonRight) {
				getMainFrame().getController(AddingLessonController.class)
						.lessButtonRightPressed(this);
			} else if (e.getSource() == playButton) {
				getMainFrame().getController(AddingLessonController.class)
						.playButtonPressed(this);
			}
		}
	}

	@Override
	public String getTitle() {
		return "Lesson: Let's Add";
	}

	@Override
	public String getBorderTitle() {
		return "Subtext";
	}
	
	@Override
	public boolean cleanupView() {
		
		// Since Back is currently the only way to navigate out of this controller
		// we set lesson to be complete when they hit back.
		getMainFrame().getController(AddingLessonController.class)
			.stopLesson(true);
		
		return true;
	}
}